package com.baibei.order.bill.deliveryquery;

import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.model.DeliveryQueryInfo;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.order.emptyview.EmptyAdapter;
import com.blankj.utilcode.utils.TimeUtils;
import com.rae.swift.Rx;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 补款取货适配器
 * Created by shenminjie on 2017/8/9.
 * mail:shenminjie@100bei.com
 */

class DeliveryQueryAdapter extends EmptyAdapter {

    /**
     * 数据源
     */
    private List<DeliveryQueryInfo> mDatas;

    DeliveryQueryAdapter() {
        isOpenEmptyView(true);
    }

    /**
     * 设置数据
     *
     * @param mDatas mDatas
     */
    void setInfos(List<DeliveryQueryInfo> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_payment_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            DeliveryQueryInfo info = mDatas.get(position);
            viewHolder.tvName.setText(String.format(viewHolder.itemView.getContext().getString(R.string.unit), info.getName(), info.getDeliveryNum()));
            viewHolder.tvDate.setText(TimeUtils.millis2String(info.getCreatedTime()));
            viewHolder.tvAmount.setText(String.format(viewHolder.itemView.getContext().getString(R.string.delivery_query_money), Rx.formatPrice(info.getDeliveryMoney())));
            String statusText = null;
            //#状态 1,审核中；2，取货成功；3，取货失败；4，已发货
            int status = info.getStatus();
            int colorResource = R.color.textPrimary;
            if (status == 1) {
                statusText = "审核中";
                colorResource = R.color.textPrimary;
            } else if (status == 2) {
                statusText = "待发货";
                colorResource = R.color.textPrimary;
            } else if (status == 3) {
                statusText = "取货失败";
                colorResource = R.color.hintPrimary;
            } else if (status == 6) {
                statusText = "已发货";
                colorResource = R.color.textPrimary;
            } else if (status == 7) {
                statusText = "取货成功";
                colorResource = R.color.buyColor;
            }
            viewHolder.tvType.setText(statusText);
            viewHolder.tvType.setTextColor(ResourcesCompat.getColor(holder.itemView.getResources(), colorResource, null));
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_amount)
        TextView tvAmount;
        @BindView(R2.id.tv_date)
        TextView tvDate;
        @BindView(R2.id.tv_type)
        TextView tvType;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
